package com.library.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.library.common.Result;
import com.library.entity.Books;
import com.library.mapper.BooksMapper;
import com.library.service.IBooksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LDH
 * @since 2022-05-12
 */
@Service
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books> implements IBooksService {

    Result result = new Result<>();


    /**
     * 功能描述: 获取所有书籍
     * @Param: []
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/5/15 19:53
     */
    @Override
    public Result getBookAll(){
        List<Books> list = list();
        return result.Huoqu_OK(list);
    }

    /**
     * 功能描述: 添加书籍
     * @Param: [books]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 16:37
     */
    @Override
    public Result addBook(Books books) {
        save(books);
        return result.Huoqu_OK(getById(books.getId()));
    }

    /**
     * 功能描述: 删除书籍
     * @Param: [id]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 18:25
     */
    @Override
    public Result deleteBook(int id){
        removeById(id);
        return result.OK("删除成功！");
    }

    /**
     * 功能描述: 修改书籍信息
     * @Param: [books]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 18:29
     */
    @Override
    public Result putBook(Books books) {
        updateById(books);
        return result.Huoqu_OK(getById(books.getId()));
    }

    /**
     * 功能描述: 模糊查询
     * @Param: [name]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 18:37
     */
    @Override
    public Result likeBook(String name) {
        QueryWrapper<Books> wrapper = new QueryWrapper<>();
        wrapper.like("bookname",name).or().like("aut",name);
        List<Books> list = list(wrapper);
        return result.Huoqu_OK(list);
    }

    /**
     * 功能描述: 批量根据id删除书籍
     * @Param: [ids]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 18:58
     */
    @Override
    public Result deleteByList(Map<String, List> ids) {
        List<Books> list = ids.get("books");
        list.forEach(this::removeById);
        return result.OK("批量删除成功！");
    }

    /**
     * 功能描述: 批量编辑书籍
     * @Param: [booksMap]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 19:16
     */
    @Override
    public Result putByList(Map<String, List<Books>> booksMap) {
        List<Books> list = booksMap.get("books");
        List<Books> list1 = new ArrayList<>();
        for (Books b : list) {
            updateById(b);
            list1.add(getById(b));
        }
        return result.Huoqu_OK(list1);
    }

    /**
     * 功能描述: 分页查询
     * @Param: [page, pageSize]
     * @Return: com.library.common.Result
     * @Author: LDH
     * @Date: 2022/6/19 19:25
     */
    @Override
    public Result booksPage(int page, int pageSize) {
        Page<Books> page1 = new Page<>(page-1,pageSize);
        page(page1);
        List<Books> list = page1.getRecords();
        return result.Huoqu_OK(list);
    }
}
